package com.qf.business.student.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.business.feign.classes.ClsFeign;
import com.qf.business.feign.score.ScoreFeign;
import com.qf.business.student.service.StudentService;
import com.qf.data.entity.Classes;
import com.qf.data.entity.Student;
import com.qf.data.mapper.StudentDao;
import com.qf.data.r.R;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;

/**
 * 学生表(Student)表服务实现类
 *
 * @author makejava
 * @since 2022-02-08 10:50:29
 */
@Service
@Slf4j
public class StudentServiceImpl extends ServiceImpl<StudentDao, Student> implements StudentService {

    @Autowired
    private ClsFeign clsFeign;

    @Autowired
    private ScoreFeign scoreFeign;


    /**
     * 保存学生信息
     * @param entity
     * @return
     */
    @Override
    @GlobalTransactional
    public boolean save(Student entity) {

        //保存学生信息
        boolean flag = super.save(entity);

        //同步的修改班级的人数信息 - 班级服务
        clsFeign.updateClsNums(entity.getCid(), 1, 0);

//        System.out.println(1/0);

        return flag;
    }

    @Override
    public Student getById(Serializable id) {
        //查询学生信息
        Student stu = super.getById(id);

        //根据班级Id，查询班级详细信息 - 远程调用班级服务
        Long cid = stu.getCid();
        R<Classes> rcls = clsFeign.getClsByCid(cid);
        log.debug("[get student] 查询学生信息连带获取班级信息 - {}", rcls);
        Classes cls = rcls.getData();
        stu.setCls(cls);

        //根据学生id，查询学分数据
        R<Double> sr = scoreFeign.getScoreBySid(stu.getId());
        Double score = sr.getData();
        stu.setScore(score);

        return stu;
    }
}
